package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.po.Download;
import com.colorlight.cloud.complete.entity.po.RcvUpdateIndex;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 升级接收卡FPGA
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class UpdateReceiver implements CompleteEntityScheduleActions {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class InputMsg implements Serializable {
        /**
         * 接收卡序号数组
         */
        private List<RcvUpdateIndex> index;

        /**
         * 是否使用慢速升级
         */
        private Boolean slowUpdate;

        /**
         * 接收卡升级文件（支持.fw | .hex文件）
         */
        private Download download;
    }

    @Data
    @NoArgsConstructor
    @Builder
    public static class OutputMsg implements Serializable {
    }

    private InputMsg input;
    private OutputMsg output;

    @Override
    public ByteString objectConvertBytes() {
        final CompleteInstruction.UpdateReceiver.InputMsg.Builder inputMsg =
                CompleteInstruction.UpdateReceiver.InputMsg.newBuilder();
        UpdateReceiver.InputMsg thisInput = this.getInput();
        inputMsg.addAllIndex(thisInput.getIndex().stream().map(RcvUpdateIndex::toProto).collect(Collectors.toList()));
        inputMsg.setSlowUpdate(thisInput.getSlowUpdate());
        inputMsg.setDownload(thisInput.getDownload().toProto());
        final CompleteInstruction.UpdateReceiver.Builder builder =
                CompleteInstruction.UpdateReceiver.newBuilder();
        builder.setInput(inputMsg);
        return builder.build().toByteString();
    }

    @Override
    public UpdateReceiver byteConvertObject(ByteString bytes) {
        this.setOutput(new OutputMsg());
        return this;
    }
}

